package com.shchuangzhi.smartguard.core.command;

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.shchuangzhi.smartguard.core.Card;
import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.NotifyMessage;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by j_hu on 5/14/17.
 */

public class SyncCardHandler extends SyncMetaCommandRespHandler {
    public SyncCardHandler(SyncMetaCommand cmd) {
        super("card.sync", cmd);
    }

    @Override
    public boolean handle(InputStream input) {
        boolean handleOk = false;
        try {
            Gson gson = new Gson();
            SyncMetaCommandRS<SyncCardListRS> syncRS = (SyncMetaCommandRS<SyncCardListRS>)gson.fromJson(
                    getString(input),
                    new TypeToken<SyncMetaCommandRS<SyncCardListRS>>(){}.getType());

            if (syncRS != null && syncRS.getData() != null) {
                getCommand().notify(NotifyMessage.info(
                        String.format("%s<%s>", Consts.INFO_SYNC_CARD, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));

                ArrayList<Card> list = new ArrayList<>();
                for(CardRS c:syncRS.getData().getList()) {
                    Card card = new Card();
                    card.setNo(c.getNo());
                    card.setHolderNo(c.getHolderNo());
                    card.setHolderType(c.getHolderType());
                    card.setSeq(c.getSeq());
                    card.setSignature(c.getSignature());
                    card.setDeleted(c.isDeleted());
                    list.add(card);
                }
                if (getCommand().getDbHelper().groupAddCards(list)) {
                    getCommand().getDbHelper().getSyncRecord().update(getName(), syncRS.getLastMetaUpdateTime());
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_CARD_OK, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                    handleOk = true;
                } else {
                    getCommand().notify(NotifyMessage.info(
                            String.format("%s<%s>", Consts.INFO_SYNC_CARD_FAIL, syncRS.getData().getList().size()), Consts.NOTIFY_DATA_SYNC, Consts.NOTIFY_DATA_SYC_VIEW_LIST));
                }
            }
        }catch(Exception e) {
            getCommand().raiseError(e);
        }
        return handleOk;
    }

    public class SyncCardListRS {
        private List<CardRS> list;
        public List<CardRS> getList() {
            return list;
        }
    }

    public class CardRS {
        private String no;
        private int seq;
        private boolean deleted;
        private String signature;

        @SerializedName("holder_no")
        private int holderNo;

        @SerializedName("holder_type")
        private int holderType;

        public String getNo() { return no; }
        public int getSeq() { return seq; }
        public String getSignature() { return signature; }
        public int getHolderNo() { return holderNo; }
        public int getHolderType() { return holderType; }
        public boolean isDeleted() { return deleted; }
    }
}
